<template>
    <div style="min-height: 500px">
        <el-button @click="dialogVisible = true" type="primary" style="margin-top: 30px">新增</el-button>
        <el-table
            :data="tableData==null?tableData:tableData.slice((currentPage-1)*pagesize,currentPage*pagesize)"
            border>
            <el-table-column fixed label="序号">
                <template slot-scope="scope">
                    {{ (scope.$index + 1) + (currentPage - 1) * pagesize }}
                </template>
            </el-table-column>
            <el-table-column prop="name" label="药物名称"></el-table-column>
            <el-table-column prop="price" label="价格"></el-table-column>
            <el-table-column fixed="right" label="操作">
                <template slot-scope="scope">
                    <el-button @click="handleAlert(scope.row)" type="text">编辑</el-button>

                    <el-button @click="handleDelete(scope.row)" type="text">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination" style="margin-top:15px;margin-left:350px">
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :page-sizes="[3, 5, 10]"
                :page-size="pagesize"
                layout="prev, pager, next,jumper"
                :total="tableData==null?0:tableData.length">
            </el-pagination>
        </div>
        <el-dialog
            title="新增药物"
            :visible.sync="dialogVisible"
            :close-on-click-modal="false"
            width="30%">
            <el-row>
                <el-col>
                    药物名称：
                    <el-input v-model="name" style="width: 300px"></el-input>
                </el-col>
                <el-col style="margin-top: 30px">
                    药物价格：
                    <el-input v-model="price" style="width: 300px"></el-input>
                </el-col>
            </el-row>
            <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addCheck()">确 定</el-button>
            </span>
        </el-dialog>
        <el-dialog
            title="修改药物"
            :visible.sync="dialogVisible1"
            :close-on-click-modal="false"
            width="30%">
            <el-row>
                <el-col>
                    药物名称：
                    <el-input v-model="name1" style="width: 300px" ></el-input>
                </el-col>
                <el-col style="margin-top: 30px">
                    药物价格：
                    <el-input v-model="price1" style="width: 300px"></el-input>
                </el-col>
                <el-col style="margin-top: 30px" v-if="false">
                    id：
                    <el-input v-model="id" style="width: 300px"></el-input>
                </el-col>
            </el-row>
            <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible1 = false">取 消</el-button>
            <el-button type="primary" @click="alertCheck()">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import axios from "axios";

export default {
    data() {
        return {
            tableData: [],
            currentPage: 1,
            pagesize: 5,
            currentaccount: "",
            currentname: "",
            dialogVisible: false,
            dialogVisible1: false,
            name: "",
            name1: "",
            price: "",
            price1: "",
            id: "",


        }
    },
    methods: {
        handleSizeChange(val) {
            this.pagesize = val;
        },
        handleCurrentChange(val) {
            this.currentPage = val;
        },
        handleClick(row) {
            console.log(row);
        },
        handleAlert(row){
            console.log("row:"+JSON.stringify(row));
            this.dialogVisible1 = true;
            this.name1 = row.name;
            this.price1 = row.price;
            this.id = row.id;
        },
        async handleDelete(row) {
            const that = this;
            await axios.post("http://localhost:8848/user/deleteMedicine", {
                id: row.id,
            }).then(function (response) {
                    console.log(response);
                }
            );
            await axios.post("http://localhost:8848/user/findAllMedicine", {}).then(function (response) {
                    console.log(response);
                    that.tableData = response.data.data;
                }
            );
        },
        async addCheck() {
            const that = this;
            await axios.post("http://localhost:8848/user/addMedicine", {
                name: this.name,
                price: this.price,
            }).then(function (response) {
                    console.log(response);
                }
            );
            await axios.post("http://localhost:8848/user/findAllMedicine", {}).then(function (response) {
                    console.log(response);
                    that.tableData = response.data.data;
                }
            );
            this.dialogVisible = false
        },
        async alertCheck(){
            const that = this;
            await axios.post("http://localhost:8848/user/alertMedicine", {
                name: this.name1,
                price: this.price1,
                id: this.id,
            }).then(function (response) {
                    console.log(response);
                }
            );
            await axios.post("http://localhost:8848/user/findAllMedicine", {}).then(function (response) {
                    console.log(response);
                    that.tableData = response.data.data;
                }
            );
            this.dialogVisible1 = false
        }

    },
    async mounted() {
        const that = this;
        await axios.post("http://localhost:8848/user/findAllMedicine", {}).then(function (response) {
                console.log(response);
                that.tableData = response.data.data;
            }
        );
    },

}
</script>
